/*
 *  Copyright (c) 2012 Alaitsev I.K.
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of this software
 *  and associated documentation files (the "Software"), to deal in the Software without restriction,
 *  including without limitation the rights to use, copy, modify, merge, publish, distribute,
 *  sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
 *  furnished to do so, subject to the following conditions:
 *
 *  The above copyright notice and this permission notice shall be included in all copies or
 *  substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
 *  NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
 *  DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#include "mainwindow.h"
#include "ui_mainwindow.h"

#include "core.h"
#include "core_visualisation_cylinder.h"
#include "core_visualisation_flat.h"

#include <QDebug>

#include <QDir>
#include <QProcess>

MainWindow::MainWindow(QWidget *parent) :
  QMainWindow(parent),
  _ui(new Ui::MainWindow),
  _core (new Core) {
  _ui->setupUi(this);
  // Необходимо присвоить значения ДО установления значений редактирующих элементов интерфейса
  _core ->external_electrode_radius_no_grade (80.);
  _core ->internal_electrode_radius_no_grade (20.);
  _core ->offset_ie_rel_ee_no_grade          (15.);
  _core ->grade_offset              (-2.);
  _core ->grade_radius              (-2.);
  // "Синхронизация" значений в редактирующих элементах интерфейса и в системе.
  _ui ->in_external_electrode_radius ->setValue (_core ->external_electrode_radius_no_grade ());
  _ui ->in_internal_electrode_radius ->setValue (_core ->internal_electrode_radius_no_grade ());
  _ui ->in_offset_ie_rel_ee          ->setValue (_core ->offset_ie_rel_ee_no_grade          ());
  _ui ->in_grade_offset              ->setValue (_core ->grade_offset());
  _ui ->in_grade_radius              ->setValue (_core ->grade_radius());
  // Сохранение изменений.
  connect(_ui ->in_external_electrode_radius,
          SIGNAL (valueChanged                       (double)),
          _core,
          SLOT   (external_electrode_radius_no_grade (double)));
  connect(_ui ->in_internal_electrode_radius,
          SIGNAL (valueChanged                       (double)),
          _core,
          SLOT   (internal_electrode_radius_no_grade (double)));
  connect(_ui ->in_offset_ie_rel_ee,
          SIGNAL (valueChanged              (double)),
          _core,
          SLOT   (offset_ie_rel_ee_no_grade (double)));
  connect (_ui ->in_grade_offset,
           SIGNAL (valueChanged (double)),
           _core,
           SLOT   (grade_offset (double)));
  connect (_ui ->in_grade_radius,
           SIGNAL (valueChanged (double)),
           _core,
           SLOT   (grade_radius (double)));
  connect (_ui ->in_magnetic_induction,
           SIGNAL (valueChanged                (double)),
           _core,
           SLOT   (magnetic_induction_no_grade (double)));
  connect (_ui ->in_potential_difference,
           SIGNAL (valueChanged                  (double)),
           _core,
           SLOT   (potential_difference_no_grade (double)));
  connect (_ui ->in_magnetic_induction_grade,
           SIGNAL (valueChanged             (double)),
           _core,
           SLOT   (grade_magnetic_induction (double)));
  connect (_ui ->in_potential_difference_grade,
           SIGNAL (valueChanged               (double)),
           _core,
           SLOT   (grade_potential_difference (double)));
  connect (_ui ->in_angle, SIGNAL(valueChanged(double)),
           _core, SLOT(initial_electrone_angle(double)));
  // Для проверки допустимости входных значений.
  connect (_core,
           SIGNAL (external_electrode_radius_valid_no_grade (double)),
           _ui ->in_external_electrode_radius,
           SLOT   (setValue                                 (double)));
  connect (_core,
           SIGNAL (internal_electrode_radius_valid_no_grade (double)),
           _ui ->in_internal_electrode_radius,
           SLOT   (setValue                                 (double)));
  connect (_core,
           SIGNAL (offset_ie_rel_ee_valid_no_grade (double)),
           _ui ->in_offset_ie_rel_ee,
           SLOT   (setValue                        (double)));
  connect (_core,
           SIGNAL (grade_offset_valid (double)),
           _ui ->in_grade_offset,
           SLOT   (setValue           (double)));
  connect (_core,
           SIGNAL (grade_radius_valid (double)),
           _ui ->in_grade_radius,
           SLOT   (setValue           (double)));
  connect (_core,
           SIGNAL (magnetic_induction_valid_no_grade (double)),
           _ui ->in_magnetic_induction,
           SLOT   (setValue                          (double)));
  connect (_core,
           SIGNAL (potential_difference_valid_no_grade (double)),
           _ui ->in_potential_difference,
           SLOT   (setValue                            (double)));
  connect (_core,
           SIGNAL (grade_potential_difference_valid (double)),
           _ui ->in_potential_difference_grade,
           SLOT   (setValue                         (double)));
  connect (_core,
           SIGNAL (grade_magnetic_induction_valid (double)),
           _ui ->in_magnetic_induction_grade,
           SLOT   (setValue                       (double)));
  // Отдельные визуализации для цилиндрического и плоского видов.
  if (_ui ->w_visualisation_holder_cylinder ->layout() == nullptr) {
    _ui ->w_visualisation_holder_cylinder ->setLayout(new QGridLayout);
  }
  Core_Visualisation_Cylinder* visualisation_cylinder = new Core_Visualisation_Cylinder;
  _ui ->w_visualisation_holder_cylinder ->layout() ->addWidget(visualisation_cylinder);
  visualisation_cylinder ->core(_core);

  // NOTE: Flat visualisation is temporary disabled.
//  if (_ui ->w_visualisation_holder_flat ->layout() == nullptr) {
//    _ui ->w_visualisation_holder_flat ->setLayout(new QGridLayout);
//  }
//  Core_Visualisation_Flat* visualisation_flat= new Core_Visualisation_Flat;
//  _ui ->w_visualisation_holder_flat ->layout() ->addWidget(visualisation_flat);
//  visualisation_flat ->core(_core);
}

MainWindow::~MainWindow() {
  delete _ui;
}

void MainWindow::on_b_process_clicked() {
  _core ->start_processing();
}
